package com.example.spring.exercise.controller;

import com.example.spring.exercise.entity.UserEntity;
import com.example.spring.exercise.repository.UserRepository;
import com.example.spring.exercise.security.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author : zhayh
 * @date : 2021-5-22 17:18
 * @description : 安全模块的使用
 */

@RestController
@RequestMapping("/api")
public class Unit6Controller {
    @Resource
    private UserRepository userRepository;

    @GetMapping("/account")
    public ResponseEntity getAccount() {
        String login = SecurityUtils.getCurrentUserLogin().get();
        Optional<UserEntity> userEntityOptional = userRepository.findByLogin(login);
        if (userEntityOptional.isPresent()) {
            return ResponseEntity.ok(userEntityOptional.get());
        } else {
            return ResponseEntity.badRequest().body("未找到登录用户信息");
        }
    }
}
